// 1 [10-85]范围内，随机取20数字，保证20个数字都不重复，存放到数组中，使用sort排序后，求出最大和最小值
let fn1 = (min, max) => {
  let list = []
  let count = 0
  for (let i = 0; i < 20; i++) {
    count++
    let num = Number.parseInt(Math.random() * (max - min + 1)) + min
    // 包含num时不执行添加操作
    if (list.includes(num)) {
      i--
      continue
    }
    list.push(num)
  }
  list.sort((x, y) => x - y)
  console.log(list, count)
  return [list[0], list[list.length - 1]]
}

// console.log(fn1(10, 85))

// 2 从26个字母中随机抽取字母拼接成一个字符串，字符串长度6-12位
let fn2 = () => {
  // 获得小写字符数组
  let list = []
  for (let i = 97; i <= 122; i++) {
    let a = String.fromCharCode(i)
    list.push(a)
  }
  for (let i = 65; i <= 90; i++) {
    let a = String.fromCharCode(i)
    list.push(a)
  }
  list.push('$', '#', '_', '!', ',')
  // 获得字符串拼接的次数
  let random = Number.parseInt(Math.random() * (12 - 6 + 1)) + 6
  let str = ''
  for (let i = 0; i < random; i++) {
    // 随机抽取list的任意下标
    let index = Math.floor(Math.random() * list.length)
    str += list[index]
  }
  console.log(str, random)
}

let fn2_1 = () => {
  // 获得字符串拼接的次数
  let random = Number.parseInt(Math.random() * (12 - 6 + 1)) + 6
  let str = ''
  for (let i = 0; i < random; i++) {
    // 随机抽取list的任意下标
    let num = Number.parseInt(Math.random() * (122 - 97 + 1)) + 97
    str += String.fromCharCode(num)
  }
  console.log(str, random)
}
fn2()

// 定时函数，一个异步函数，不会像同步代码阻塞执行
setInterval(function () {
  document.getElementsByClassName('box')[0].innerHTML = formatDate()
}, 1000)
